using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace OnlineAnadolu
{
    public partial class OzellikEkle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["usertype"].ToString() != "1")
            {
                Response.Redirect("HataSayfasi.aspx");
            }
            if (!IsPostBack)
            {
                FillAuthors(Menu1.Items[0]);

            }
            Menu1.DataBind();

        }
        void FillAuthors(MenuItem node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where parentid is null and status=1", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                    // newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildItems.Add(newNode);

                    FillTitlesForAuthors(newNode);

                }
            }
        }

        int yolbul(int childid)
        {


            Database db = new Database();

            string query1 = "Select * from categories where categoryid=" + childid + "";
            DataTable dt = db.select(query1);

            foreach (DataRow dr in dt.Rows)
            {
                Label2.Text = ">>>" + dr["categoryname"].ToString() + Label2.Text;
                string query2 = "SELECT attributevalues.attributeid,attributetypes.datatype, attributes.attributename FROM attributes INNER JOIN attributevalues ON attributes.attributeid = attributevalues.attributeid INNER JOIN attributetypes ON attributes.type = attributetypes.attributecode WHERE(attributes.categoryid = " + dr["categoryid"].ToString() + ") group by attributevalues.attributeid,attributetypes.datatype,attributes.attributename";
                DataTable dt2 = new DataTable();
                dt2 = db.select(query2);

                if (dr["parentid"].ToString() == "")
                    return 0;

                yolbul(Convert.ToInt32(dr["parentid"]));
            }

            return 0;

        }


        void FillTitlesForAuthors(MenuItem node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "' and status=1", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();
                    //    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.PopulateOnDemand = false;
                    //   newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildItems.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
       //     lb_EklemeKategori.Text = Menu1.SelectedItem.Text + " in altina ekliyorsunuz";
            Label2.Text = "";
            yolbul(Convert.ToInt32(Menu1.SelectedValue));
            
        }

        protected void bt_Kaydet_Click(object sender, EventArgs e)
        {
            string simage = "",sr="";
            string s = "INSERT INTO attributes(type, categoryid, attributename, status) VALUES ('"+DropDownList1.SelectedValue+"','"+Menu1.SelectedValue+"',N'"+tb_Ozellik.Text+"',1)";
            Database db = new Database();
            db.insert(s);
            ////////////////
            //s = "select top 1 attributeid from attributes order by attributeid desc";
            //DataTable dtid = new DataTable();
            //dtid = db.select(s);
            //foreach (DataRow dr11 in dtid.Rows)
            //{
            //    simage = dr11["productid"].ToString();
            //}
            ////////
            //sr = "INSERT INTO attributes(attributeid, categoryid, description, status) VALUES ('" + simage + "','" + Menu1.SelectedValue + "',N'" + tb_Ozellik.Text + "',1)";
            lb_Uyari.Text = "Ozellik kaydedildi";
           
        }
    }
}
